<template>
  <div class="mask">
    <div class="mask-con">
      <h2 style="padding: 20px">叩丁狼商品管理系统</h2>
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        status-icon
        :rules="rules"
        class="demo-ruleForm"
      >
        <el-form-item prop="username">
          <el-input
            v-model="ruleForm.username"
            type="text"
            placeholder="请输入账号"
            autocomplete="off"
            size="large"
          />
        </el-form-item>
        <el-form-item prop="paw">
          <el-input
            v-model="ruleForm.paw"
            type="password"
            placeholder="请输入密码"
            autocomplete="off"
            size="large"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="resetForm()"
            style="width: 100%"
            size="large"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import * as http from "../../request/http";
import Cookies from "js-cookie";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";
import { useUserStore } from "@/store/modules/user";

const ruleFormRef = ref();
const router = useRouter();
const userStore = useUserStore();
const ruleForm = reactive({
  username: "admin",
  paw: "123456",
});
function checkUserVali(
  rules: unknown,
  value: string | undefined,
  callback: (msg?: string) => void
) {
  if (!value) {
    return callback("账号不能为空");
  } else {
    return callback();
  }
}
function checkPawVali(
  rules: unknown,
  value: string | undefined,
  callback: (msg?: string) => void
) {
  if (!value) {
    return callback("密码不能为空");
  } else {
    return callback();
  }
}
//校验规则
const rules = reactive({
  username: [
    {
      validator: checkUserVali,
      trigger: "blur",
    },
  ],
  paw: [
    {
      validator: checkPawVali,
      trigger: "blur",
    },
  ],
});
// 点击方法
async function resetForm() {
  //获取表单实例对象
  // ruleFormRef.value.validate(async (isValid: boolean) => {
  //   if (isValid) {
  //     console.log("通过校验");
  //     let loginRes = await http.adminLogin({
  //       password: ruleForm.paw,
  //       username: ruleForm.username,
  //     });
  //   } else {
  //     console.log("没有通过校验");
  //   }
  // });

  // ruleFormRef.value
  //   .validate()
  //   .then(() => {
  //     console.log("通过校验");
  //   })
  //   .catch(() => {
  //     console.log("没有通过校验");
  //   });

  try {
    await ruleFormRef.value.validate();
    console.log("通过校验");
    const loginRes = await http.admin.adminLogin({
      password: ruleForm.paw,
      username: ruleForm.username,
    });
    if (loginRes.code === 200) {
      //存储到cookie
      Cookies.set("token", loginRes.data.tokenHead + loginRes.data.token);
      // //请求用户数据
      // const adminData = await http.admin.getAdminInfo();
      // //存储pinia
      // userStore.updateMenus(adminData.data.menus);
      //跳转/homepage
      router.push("/index");
      ElMessage.success("登录成功");
    } else {
      ElMessage.error(loginRes.message || "登录请求失败");
    }
  } catch (error) {
    console.log("没有通过校验");
  } finally {
    console.log("finally");
  }
}
</script>

<style scoped>
.mask {
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  background-color: #2d3a4b;
}
.mask-con {
  width: 45%;
  margin: 200px auto;
}
h2 {
  color: #fff;
  font-size: 24px;
  text-align: center;
}

:deep(.el-form-item) {
  background-color: rgba(0, 0, 0, 0.1);
  border: 1pxsolidhsla (0, 0%, 100%, 0.1);
  border-radius: 5px;
}

:deep(.el-input__inner) {
  padding: 0 10px;
  height: 50px;
}
</style>
